package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.CustomException;
import com.itheima.reggie.ResultInfo;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.domain.SetmealDish;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

/**
 * 套餐业务
 */
@Service
@Transactional
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;


    //分页查询
    @Override
    public Page<Setmeal> findByPge(Integer pageNum, Integer pageSize, String name) {

        //构建分页条件
        Page<Setmeal> page = new Page<>(pageNum, pageSize);

        //设置查询条件
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(name),Setmeal::getName, name);

       page = (Page<Setmeal>) setmealMapper.selectPage(page, wrapper);


       //遍历列表，得到每个套餐
        if (CollectionUtil.isNotEmpty(page.getRecords())) {
            for (Setmeal setmeal : page.getRecords()) {
                Category category = categoryMapper.selectById(setmeal.getCategoryId());
                setmeal.setCategoryName(category.getName());

                //4. 根据套餐id去setmeal_dish中查询套餐信息
                LambdaQueryWrapper<SetmealDish> wrapper1 = new LambdaQueryWrapper<>();
                wrapper1.eq(SetmealDish::getDishId, setmeal.getId());
                List<SetmealDish> setmealDishList = setmealDishMapper.selectList(wrapper1);
                setmeal.setSetmealDishes(setmealDishList);
            }
        }

        return page;
    }


    //保存
    @Override
    public void save(Setmeal setmeal) {
        //将基本信息保存到setmeal表
        setmealMapper.insert(setmeal);

        //获取菜品列表
        List<SetmealDish> setmealDishes = setmeal.getSetmealDishes();
        if (CollectionUtil.isNotEmpty(setmealDishes)) {
            int i = 0;
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(setmeal.getId()); //设置套餐id
                setmealDish.setSort(i++);
                setmealDishMapper.insert(setmealDish);
            }
        }
    }

    //删除
    @Override
    public void delete(List<Long> ids) {
        //判断是否在在售状态
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Setmeal::getStatus, 1).in(Setmeal::getId, ids);
        Integer count = setmealMapper.selectCount(wrapper);

        if (count > 0) {
            throw new CustomException("有套餐在销售状态，删除失败！");
        }

        //删除菜品信息
        LambdaQueryWrapper<SetmealDish> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.in(SetmealDish::getSetmealId, ids);
        setmealDishMapper.delete(wrapper1);

        //删除
        setmealMapper.deleteBatchIds(ids);
    }


    //根据id查询
    @Override
    public Setmeal findById(Long id) {

        //根据id获取套餐基本信息
        Setmeal setmeal = setmealMapper.selectById(id);
        //根据分类id去分类表获取分类名字
        Category category = categoryMapper.selectById(setmeal.getCategoryId());
        setmeal.setCategoryName(category.getName());

        //根据套餐id去setmeal_dish中查询套餐信息
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, setmeal.getId());
        List<SetmealDish> setmealDishList = setmealDishMapper.selectList(wrapper);
        setmeal.setSetmealDishes(setmealDishList);

        return setmeal;
    }


    //修改
    @Override
    public void update(Setmeal setmeal) {
        //根据套餐id更新基本信息
        setmealMapper.updateById(setmeal);

        //根据setmeal_id删除setmeal_dish中得相关信息(条件删除)
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, setmeal.getId());
        setmealDishMapper.delete(wrapper);

        //重新添加
        List<SetmealDish> setmealDishes = setmeal.getSetmealDishes();
        if (CollectionUtil.isNotEmpty(setmealDishes)) {
            int i = 0;
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(setmeal.getId()); //设置套餐id
                setmealDish.setSort(i++);
                setmealDishMapper.insert(setmealDish);
            }
        }
    }


    //修改售卖状态
    @Override
    public void updateStatus(Integer status,List<Long> ids) {

        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Setmeal::getId, ids);

        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        setmealMapper.update(setmeal, wrapper);

    }


    //查询分类套餐
    @Override
    public List<Setmeal> findList(Long categoryId, Integer status) {
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Setmeal::getCategoryId, categoryId).eq(Setmeal::getStatus, status);
        return setmealMapper.selectList(wrapper);
    }


    //查询套餐菜品
    @Override
    public List<Dish> findSetmealDish(Long id) {
        //先用套餐id查询出所有菜品
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> setmealDishList = setmealDishMapper.selectList(wrapper);

        //遍历菜品集合
        List<Dish> dishList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(setmealDishList)) {
            for (SetmealDish setmealDish : setmealDishList) {
                Dish dish = dishMapper.selectById(setmealDish.getDishId());
                dish.setCopies(setmealDish.getCopies());
                dishList.add(dish);
            }
        }

        return dishList;
    }
}

